package com.test.controller;

import com.test.bean.ReStudent;
import com.test.bean.Result;
import com.test.bean.Student;
import com.test.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

@Controller
@RequestMapping("reward")

public class RewardController {
    @Autowired
    private UserService userService;

//    学生奖助信息单条录入
    @ResponseBody
    @RequestMapping(value = "add",method = RequestMethod.POST)
    public Result AddReward(ReStudent restudent, HttpSession session, HttpServletResponse response){
        Result result = new Result();
        try {
            userService.addReStudent(restudent);
            result.setMessage("录入成功");
            result.setCode(200);
        }catch (Exception e){
            e.printStackTrace();
            result.setMessage("录入失败");
            result.setCode(400);//异常
        }
        return result;
    }

//    学生奖助信息excel录入

//    奖助信息证明图片上传至数据库中
//    @ResponseBody
//    @RequestMapping(value = "image",method = RequestMethod.POST)
//    public Result ImageReward(HttpServletRequest request, MultipartFile upload){
//
//    }

//    奖助学金信息查询   返回对象数组6
//    @ResponseBody
//    @RequestMapping(value = "query",method = RequestMethod.GET)
//    public ReStudent

//    奖助学金信息修改
    @ResponseBody
    @RequestMapping(value = "change",method = RequestMethod.PUT)
    public Result ChangeReward(ReStudent restudent,HttpSession session){
        Result result = new Result();
        ReStudent restudent1 = userService.queryRestudent(restudent);
        if(restudent1 != null) {
            userService.changeRestudent(restudent);
            //调用服务层来提供修改服务
            //        System.out.println("修改成功！");
            result.setMessage("修改成功");
            result.setCode(200);
        }else{
            result.setMessage("修改失败");
            result.setCode(400);
        }
        return result;
    }

//    奖助学金信息删除
    @ResponseBody
    @RequestMapping(value = "delete",method = RequestMethod.DELETE)
    public Result DeleteReward(ReStudent restudent,HttpSession session,HttpServletResponse response){
        Result result = new Result();
        ReStudent restudent1 = userService.queryRestudent(restudent);
        if(restudent1 != null){
            try {
                userService.deleteReStudent(restudent);
                result.setMessage("删除成功");
                result.setCode(200);
            }catch (Exception e){
                e.printStackTrace();
                result.setMessage("删除失败");
                result.setCode(400);//异常
            }
        }
        else {
            result.setMessage("删除失败");
            result.setCode(400);//异常
        }
        return result;
    }
}
